<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form action="index.html" method="post" class="definewidth m20">
    <input type="hidden" name="mId" value=""/>
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">资源名称</td>
            <td><input type="text" name="mName" value="挂号信息管理"/></td>
        </tr>
        <tr>
            <td class="tableleft">url</td>
            <td><input type="text" name="mUrl" value="query.action"/></td>
        </tr>
        <tr>
            <td class="tableleft">是否有效</td>
            <td>
                <input type="radio" name="mState" value="0" checked/> 有效
                <input type="radio" name="mState" value="1"/> 无效
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button id="updateBtn" class="btn btn-primary" type="button">更新
                </button>&nbsp;&nbsp;<button
                    type="button"
                    class="btn btn-success"
                    name="backid"
                    id="backid">
                返回列表
            </button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>String.prototype.GetValue = function (para) {
    let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
    let r = this.substr(this.indexOf("\?") + 1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function update() {
    let mId = $("input[name='mId']").val();
    let mName = $("input[name='mName']").val();
    let mUrl = $("input[name='mUrl']").val();
    let mState = $("input[name='mState']:checked").val();
    // alert(mId)
    // alert(mName)
    // alert(mUrl)
    // alert(mState)
    $.ajax({
        type: "POST",
        url: "/resource/save",
        dataType: "json",
        data: {
            mId,
            mName,
            mUrl,
            mState
        },
        success: function (res) {
            alert(res.msg);
        }
    })
}

$(function () {
    let url = document.location.toString();
    let mId = url.GetValue("mId");
    // 通过 ID 获取数据
    $.ajax({
        type: "GET",
        url: "/resource/" + mId,
        success: function (res) {
            $("input[name='mId']").val(res.obj.mId);
            $("input[name='mName']").val(res.obj.mName);
            $("input[name='mUrl']").val(res.obj.mUrl);
            let mState = res.obj.mState;
            if (mState === 0) {
                $("input[name='mState']").get(0).checked = true;
            } else if (mState === 1) {
                $("input[name='mState']").get(1).checked = true;
            }
        }
    })

    $("#updateBtn").click(function () {
        // alert("had called!")
        update();
    })

    $('#backid').click(function () {
        window.location.href = "index.html";
    });
});
</script>